import fs from 'node:fs'
import path from 'node:path'
import process from 'node:process'

// The first command line argument will be the target directory ('public' or 'dist')
const targetDir = process.argv[2]

if (!targetDir) {
  console.error(
    'Target directory not specified. Usage: node scripts/generate-version.js <directory>',
  )
  process.exit(1)
}

const versionInfo = {
  version: new Date().toISOString(),
}

const targetPath = path.resolve(process.cwd(), targetDir)

// Ensure the target directory exists
if (!fs.existsSync(targetPath)) {
  fs.mkdirSync(targetPath, { recursive: true })
}

fs.writeFileSync(path.resolve(targetPath, 'version.json'), JSON.stringify(versionInfo))

console.log(`Version file created at ${targetDir}/version.json with version ${versionInfo.version}`)
